import { route, GET, POST } from 'awilix-express';

@route('/emails')
export default class EmailsApi {

  constructor({ emailsService }) {
    this.emailsService = emailsService;
  }

  @route('/getEmailsAll')
  @POST()
  async getEmailsAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.emailsService.getEmailsAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getEmailslist')
  @POST()
  async getEmailslist(req, res) {
    const { t_email, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.emailsService.getEmailslist({t_email, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  @route('/addEmails')
  @POST()
  async addEmails(req, res) {
    const { 
      t_email,
      mark
     } = req.body;
    const { success, message } = await this.emailsService.addEmails({
      t_email,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delEmails')
  @POST()
  async delEmails(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.emailsService.delEmails({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  @route('/updateEmails')
  @POST()
  async updateEmails(req, res) {
    const {
      id,
      t_email
    } = req.body;
    const { success, message } = await this.emailsService.updateEmails({
      id,
      t_email
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}